var data = {
  step: 5,
  title: "xiazaiIDE",
  label: "huanjingzhunbei    2/2",
  src: "../../static/images/miniProgram8.png",
  hint_flag1: 0,
  hint1: "微信小程序",
  hint_src1: "../miniProgram/miniProgram",
  content: "此时一个简单但是完整的云开发表单应用就完成啦！在输入框中输入‘mahuateng’， 点击提交按钮，稍等一两秒，刷新云开发控制台的页面即可看到数据库中的tag为1的记录的content字段已被成功更新！\n\n恭喜您！您已完成云开发入门教程！",
  question_flag: 1,
  question: "若被触发的函数有一个参数，此参数的含义是什么?",
  result: 0,
  answer: 0,
  select: "",
  options: [
    "自定义参数，含义由自己定义。",
    "触发此函数的组件元素名。",
    "触发此函数的动作名。",
    "触发此函数的事件的相关信息。"
  ],
  right_option: "4",
  hint_flag2: 1,
  hint2: "学习方法及工具：思维导图",
  hint_src2: "../miniProgram/miniProgram",
  last: "../miniProgram/miniProgram",
  next: "../miniProgram/miniProgram"
}

export function jump1() {
  var that = this
  wx.navigateTo({
    url: that.data.hint_src1,
  })
}

export function jump2() {
  var that = this
  wx.navigateTo({
    url: that.data.hint_src2,
  })
}

export function radioChange(e) {
  this.setData({
    select: e.detail.value
  })
}

export function answer() {
  var that = this
  that.setData({
    answer: 1
  })

  wx.showLoading({
    title: '提交中',
  })

  wx.login({
    success(resout) {
      if (resout.code) {
        console.log(111)
        // 发起网络请求
        var result = 0
        var bingo = ""
        if (that.data.select == that.data.right_option) {
          result = 1
          bingo = "right"
        } else {
          result = 2
          bingo = "wrong"
        }

        wx.request({
          url: 'https://www.ourspark.org/helloworld/add',
          data: {
            code: resout.code,
            content: that.data.title,
            step: that.data.step,
            stepAnswer: that.data.select,
            result: bingo
          },
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(res) {
            console.log(res)
            if (res.data.error_no == 2) {
              wx.showModal({
                title: "哎呀~",
                content: res.data.error_msg,
              })
            } else if (res.data.error_no == 1) {
              wx.hideLoading();

              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 1000
              });

              getApp().globalData.step = that.data.step
              that.setData({
                answer: 0,
                result
              });
            }
          },
          fail: function(res) {

          }
        })
      } else {
        console.log('登录失败！' + resout.errMsg)
      }
    }
  })
}

export function toLast() {
  var that = this
  wx.redirectTo({
    url: that.data.last,
  })
}

export function toNext() {
  var that = this
  if (typeof that.data.step == "string") {
    wx.redirectTo({
      url: that.data.next,
    })
  } else if (that.data.question_flag == 1) {
    if (getApp().globalData.step >= this.data.step) {
      wx.redirectTo({
        url: that.data.next,
      })

    } else {
      wx.showModal({
        title: '提示~',
        content: '您还没有完成本页的练习呦！',
      })
    }
  } else if (that.data.question_flag == 0) {
    wx.login({
      success(resout) {
        if (resout.code) {
          console.log(111)
          // 发起网络请求
          wx.request({
            url: 'https://www.ourspark.org/helloworld/add',
            data: {
              code: resout.code,
              content: that.data.title,
              step: that.data.step,
              stepAnswer: "",
              result: ""
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(res) {
              console.log(res)
              if (res.data.error_no == 2) {
                wx.showModal({
                  title: "哎呀~",
                  content: res.data.error_msg,
                })
              } else if (res.data.error_no == 1) {

                getApp().globalData.step = that.data.step
                wx.redirectTo({
                  url: that.data.next,
                })

              }
            },
            fail: function(res) {

            }
          })
        } else {
          console.log('登录失败！' + resout.errMsg)
        }
      }
    })
  }

}

export function imgYu1() {
  var that = this
  wx.getImageInfo({
    src: that.data.src,
    success: function(res) {
      var path = res.path.split('/')
      wx.previewImage({
        urls: ["https://www.ourspark.org/helloworld/image?url=" + path[path.length - 1]]
      })
    }
  })
}

export function imgYu2() {

  wx.getImageInfo({
    src: "../../static/images/cloudFunction2.png",
    success: function(res) {
      var path = res.path.split('/')
      wx.previewImage({
        urls: ["https://www.ourspark.org/helloworld/image?url=" + path[path.length - 1]]
      })
    }
  })
}

// Page({
//   data,
//   jump1,
//   radioChange,
//   answer,
//   jump2,
//   toLast,
//   toNext
// })